package com.xw.studyone.progress

import android.app.Service
import android.content.Intent
import android.os.*
import android.util.Log

class MyService2 : Service() {

    private val mMessenger by lazy { Messenger(ServiceHandler()) }

    inner class ServiceHandler: Handler(Looper.getMainLooper()) {

        override fun handleMessage(msg: Message) {

            when(msg.what) {

                0 -> {
                    val bundle = msg.data
                    if (bundle != null) {

                        val mmsge = bundle.getString("type")
                        Log.d("info","=====Service获取binder通信====" + mmsge)

                        val messenger = msg.replyTo
                        val message = Message.obtain(null,1)
                        val bundle1 = Bundle()
                        bundle1.putString("kep","这是Service返回的响应消息")
                        message.data = bundle1

                        messenger.send(message)
                    }
                } else ->   super.handleMessage(msg)

            }
        }
    }

    override fun onBind(intent: Intent): IBinder {
        //以下是binder通信返回的对象
        return mMessenger.binder
    }
}